package week_04.coef_linklist;

import week_04.LinkList;
import week_04.LinkListString;

public class coef_linklist {
    public static void main(String[] args) throws Exception{
        LinkListString<Integer> s1 = new LinkListString<>();
        LinkListString<Integer> s2 = new LinkListString<>();
        LinkListString<Integer> s3 = new LinkListString<>();
        int x = 1;
//        f(x)=5*Math.pow(x,4)+4*Math.pow(x,2)+6*x;
//        g(x)=6*Math.pow(x,5)+3*Math.pow(x,3)+4*Math.pow(x,2)+10;
        s1.insert(5, 4);
        s1.insert(4, 2);
        s1.insert(6, 1);
        s2.insert(6, 5);
        s2.insert(3, 3);
        s2.insert(4, 2);
        s2.insert(10, 0);
        for (int i = 0; i < s1.length(); i++) {
            System.out.println(s1.getCoef(i));
            System.out.println(s1.getPow(i));
            System.out.println("——————————————————————————");
        }
        System.out.println("---——————--——————————--——————----————---————----");
        for (int i = 0; i < s2.length(); i++) {
            System.out.println(s2.getCoef(i));
            System.out.println(s2.getPow(i));
            System.out.println("——————————————————————————");
        }
        System.out.println("---——————--——————————--——————----————---————----");
        int length;
        if (s1.length() > s2.length()) {
            length = s1.length();
        } else {
            length = s2.length();
        }
        for (int i = 0; i < length; i++) {
            if (i<3){
                s3.insert(s2.getCoef(i), s2.getPow(i));
                s3.insert(s1.getCoef(i), s1.getPow(i));
            }else {
                s3.insert(s2.getCoef(i), s2.getPow(i));
            }

        }
        for (int i = 0; i < s3.length(); i++) {
            for (int j = i+1; j < s3.length() - 1; j++) {
                if (s3.getPow(i).equals(s3.getPow(j))) {
                    s3.insert(s3.getCoef(i) + s3.getCoef(j), s3.getPow(i));
                }
            }
        }
        for (int i=0;i< s3.length();i++){
            for (int j = i+1; j < s3.length() - 1; j++) {
                if (s3.getPow(i).equals(s3.getPow(j))) {
                    s3.remove(i);
                    s3.remove(j-1);
                }
            }
        }
        for (int i = 0; i < s3.length(); i++) {
            System.out.println(s3.getCoef(i));
            System.out.println(s3.getPow(i));
            System.out.println("——————————————————————————");
        }

    }
}
